package com.slkj.shilixiaoyuanapp.ui.teacher.tool.officialSeal;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.google.gson.Gson;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolOfficialSealChoseEntity;
import com.slkj.shilixiaoyuanapp.entity.BasePickerTypeEntity;
import com.slkj.shilixiaoyuanapp.entity.code.ToolCode;
import com.slkj.shilixiaoyuanapp.entity.commitdata.OfficialSealCommitData;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.common.CommonToolHistoryActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.util.TimeUtils;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.PickerViewProvider;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.tkk.api.RxEventProcessor;
import com.tkk.api.annotation.Recipient;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SPR_FINSH;
import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.NO_CHOSE;

@ActivityInfo(layout = R.layout.activity_official_seal, title = "公章", rightHint = "历史记录")
public class ApplyOfficialSealActivity extends BaseActivity {
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_file_type)
    TextView tvFileType;
    @BindView(R.id.tv_seal_type)
    TextView tvSealType;
    @BindView(R.id.tv_spr)
    TextView tvSpr;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_nums)
    EditText editNums;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_now_day_time)
    TextView tvNowDayTime;
    List<Integer> sqrIds = new ArrayList<>();

    private ArrayList<PeopleEntity> peopleEntities = new ArrayList<>();
    private String time;
    private int sealTypeId = NO_CHOSE;
    //审批人和用章类型数据
    ToolOfficialSealChoseEntity choseData;
    public static final String tag = "ApplyOfficialSealActivity";

    @Override
    protected void init() {
        RxEventProcessor.get().bind(this);
        tvUserName.setText(UserRequest.getInstance().getUser().getUser().getUName());
        tvNowDayTime.setText(TimeUtils.getNowNyr());
    }

    @Override
    protected void onRightTitleClick() {
        CommonToolHistoryActivity__JumpCenter.builder(this)
                .setNetToolType(8)
                .setToolType(ToolCode.TOOL_GZ).create().go();    }

    @OnClick(R.id.tv_up)
    void upData() {
        OfficialSealCommitData commitData = new OfficialSealCommitData();
        if (TextUtils.isEmpty(time)) {
            showToast("请选择时间");
            return;
        }
        commitData.setApplytime(time);
        String name = editName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请输入文件名称");
            return;
        }
        commitData.setFilename(name);
        String num = editNums.getText().toString();
        if (TextUtils.isEmpty(num)) {
            showToast("请输入文件份数");
            return;
        }
        commitData.setFilecopies(num);
        String fileType = tvFileType.getText().toString();
        if (TextUtils.isEmpty(fileType)) {
            showToast("请选择文件类型");
            return;
        }
        commitData.setFiletype(fileType);

        if (sealTypeId == NO_CHOSE) {
            showToast("请选择印章类型");
            return;
        }
        commitData.setType(String.valueOf(sealTypeId));
        String context = editContent.getText();
        if (TextUtils.isEmpty(context)) {
            showToast("请输入会议内容");
            return;
        }
        commitData.setBody(context);
        if (sqrIds.size() == 0) {
            showToast("请选择审批人");
            return;
        }
        List<OfficialSealCommitData> dataList = new ArrayList<>();
        Gson gson = new Gson();
        dataList.add(commitData);
        String jsonData =  gson.toJson(dataList);
        HttpHeper.get().toolService()
                .commitOfficialSeal(jsonData,sqrIds.toString())
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(ApplyOfficialSealActivity.this,data);
                        clearContent();
                    }
                });
    }

    @OnClick(R.id.layout_chose_time)
    public void onLayoutChoseTimeClicked() {
        PickerViewProvider.getDayTimePicker(this, (date, v) -> {
            time = TimeUtils.dateToStr(date);
            tvTime.setText(time);
        }).show();
    }

    /**
     * 文件类型:原件复印件。
     * 接口传String
     */
    @OnClick(R.id.layout_chose_file_type)
    public void onLayoutChoseFileTypeClicked() {
        List<BasePickerTypeEntity> typeEntityList = new ArrayList<>();
        typeEntityList.add(new BasePickerTypeEntity("原件"));
        typeEntityList.add(new BasePickerTypeEntity("复印件"));
        PickerViewProvider.getCommonPicker(this, typeEntityList, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvFileType.setText(typeEntityList.get(options1).getName());
            }
        }).show();
    }

    /**
     * 印章类型
     */
    @OnClick(R.id.layout_chose_seal_type)
    public void onLayoutChoseSealTypeClicked() {
        if (choseData == null) {
            getChoseData(1);
        } else {
            showTypePicker(choseData.getType());
        }
    }

    /**
     * 显示印章类型选择器
     *
     * @param sealTypes
     */
    private void showTypePicker(List<ToolOfficialSealChoseEntity.TypeEntity> sealTypes) {
        PickerViewProvider.getCommonPicker(this, sealTypes, (options1, options2, options3, v) -> {
            tvSealType.setText(sealTypes.get(options1).getName());
            sealTypeId = sealTypes.get(options1).getId();
        }).show();
    }

    /**
     * 获取印章类型和审批人数据
     *
     * @param type 1:印章类型，2：审批人
     */
    private void getChoseData(int type) {
        HttpHeper.get().toolService()
                .getTypeAndAuditor()
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<ToolOfficialSealChoseEntity>(true, this) {
                    @Override
                    public void onCallBackSuccess(ToolOfficialSealChoseEntity data) {
                        choseData = data;
                        peopleEntities.addAll(choseData.getAuditor());
                        if (type == 1) {
                            showTypePicker(choseData.getType());
                        } else {
                            gotoChosePeople();
                        }
                    }
                });
    }

    /**
     *
     */
    @OnClick(R.id.layout_chose_spr)
    public void onLayoutChoseSprClicked() {
        if (choseData == null) {
            getChoseData(2);
        } else {
            gotoChosePeople();
        }

    }

    private void gotoChosePeople() {
        ChosePeopleActivity__JumpCenter
                .builder(this)
                .setData(peopleEntities)
                .setTag(tag)
                .create()
                .go();
    }

    @Recipient(tag = tag + CHOSE_SPR_FINSH)
    void onChoseObjFinsh(int code, String info, List<PeopleEntity> peopleEntityList, List<Integer> checkIds) {
        tvSpr.setText(checkIds.size() == 0 ? "" : info);
        sqrIds.clear();
        sqrIds.addAll(checkIds);
        peopleEntities.clear();
        peopleEntities.addAll(peopleEntityList);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxEventProcessor.get().unBind(this);
    }

}
